package com.hjm.common.utils.excel;

import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import lombok.Data;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SheetUtils
 * @Deacription TODO
 * @Author Gdd
 * @Version 1.0
 **/
@Data
public class SheetUtils {





    /**
     * 写入excel
     * @Param [filePath 文件路径, sheetName sheet名称, titleName 表头名称, data 结果集]
     * @Return void
     */
    public static boolean createExcel(String filePath, String sheetName, Map<String, String> titleName, List<Map<String, Object>> data) {
        //大数据生成
        BigExcelWriter writer = ExcelUtil.getBigWriter(filePath,sheetName);
        try{
                //获取第一个sheet
                Sheet sheet = writer.getSheet();
                //默认宽度
                sheet.setDefaultColumnWidth(30);
                //设置表头转换
                writer.setHeaderAlias(titleName);
                // 一次性写出内容，使用默认样式
                writer.write(data);
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }finally {
                writer.close();
           }
        return true;
    }


    public static void main(String[] args) {
        //读取文件





        List<Map<String, Object>> list = new ArrayList<>();
        Map<String,String> titleName = new HashMap<>();
        titleName.put("agent_num","代理商编号");
        titleName.put("agent_name","代理商名称");
        titleName.put("mer_no","商户编号");
        titleName.put("mer_name","商户名称");
        titleName.put("phone","电话");

        Map<String,Object> data = new HashMap<>();
        data.put("name","goods");
        data.put("sex","1");


        createExcel("d:/代理商户信息.xlsx","信息",titleName,list);




    }




}
